package com.easytouch.vmhdl.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.easytouch.vmhdl.R;


public class RingView extends SurfaceView implements SurfaceHolder.Callback, Runnable {

    private SurfaceHolder mHolder;
    private boolean mIsDrawing;
    private Canvas mCanvas;
    private int mHeight = 0;

    private int mFirstColor;
    private int mSecondColor;
    private int mCircleWidth;
    private Paint mPaint;
    private int mProgress;
    private int mSpeed;
    private boolean isNext = false;

    private Bitmap waveBitmap;
    private RectF oval;
    private int centre;
    private int radius;

    public RingView(Context context) {
        super(context);
        initView();
    }

    public RingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public RingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setFormat(PixelFormat.TRANSPARENT);
        setZOrderOnTop(true);
        setFocusable(true);
        setFocusableInTouchMode(true);
        setKeepScreenOn(true);

        mFirstColor = Color.parseColor("#1C2D3A");
        mSecondColor = Color.parseColor("#03E0F4");
        mCircleWidth = 45;
        mSpeed = 1;
        mPaint = new Paint();
    }


    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        mIsDrawing = true;
        new Thread(this).start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        waveBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ring_detect_new);
        waveBitmap = scaleBitmap(waveBitmap, width);
//        mBitmapHeight = waveBitmap.getHeight();
        mHeight = height;

        // 获取圆心的x坐标
        centre = mHeight / 2;
        //半径
        radius = (centre - mCircleWidth / 2) - 2;
        mPaint.setStrokeWidth(mCircleWidth); // 设置圆环的宽度
        mPaint.setAntiAlias(true); // 消除锯齿
        mPaint.setStyle(Paint.Style.STROKE); // 设置空心
        // 用于定义的圆弧的形状和大小的界限
        oval = new RectF(centre - radius, centre - radius, centre + radius, centre + radius);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        mIsDrawing = false;
    }

    @Override
    public void run() {

        while (mIsDrawing) {
            long start = System.currentTimeMillis();

//            mProgress++;
            mProgress = mProgress + 3;
            if (mProgress >= 360) {
                mProgress = 0;
                if (!isNext)
                    isNext = true;
                else
                    isNext = false;
            }

            draw();

            try {
                Thread.sleep(mSpeed);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

//            long end = System.currentTimeMillis();
//            if (end - start < 50) {
//                try {
//                    Thread.sleep(50 - (end - start));
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }

        }
    }

    private void draw() {
        try {
            mCanvas = mHolder.lockCanvas();
            mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

//            int centre = getWidth() / 2; // 获取圆心的x坐标


            if (!isNext) {// 第一颜色的圈完整，第二颜色跑
                mPaint.setColor(mFirstColor); // 设置圆环的颜色
                mCanvas.drawCircle(centre, centre, radius, mPaint); // 画出圆环
                mPaint.setColor(mSecondColor); // 设置圆环的颜色
                if (mProgress > 270) {
                    mCanvas.drawArc(oval, mProgress - 270 - 90, 270, false, mPaint); // 根据进度画圆弧
                } else {
                    mCanvas.drawArc(oval, -90, mProgress, false, mPaint); // 根据进度画圆弧
                }

            } else {
                if (mProgress < 90) {
                    mProgress = 90;
                }
                mPaint.setColor(mSecondColor); // 设置圆环的颜色
                mCanvas.drawCircle(centre, centre, radius, mPaint); // 画出圆环
                mPaint.setColor(mFirstColor); // 设置圆环的颜色
                mCanvas.drawArc(oval, -90, mProgress, false, mPaint); // 根据进度画圆弧
            }

            mCanvas.drawBitmap(waveBitmap, 0, 0, null);
        } catch (Exception e) {
        } finally {
            if (mCanvas != null) {
                mHolder.unlockCanvasAndPost(mCanvas);
            }
        }

    }


    /**
     * 根据给定的宽,进行等比例拉伸
     *
     * @param origin   原图
     * @param newWidth 新图的宽
     * @return new Bitmap
     */
    private Bitmap scaleBitmap(Bitmap origin, int newWidth) {
        if (origin == null) {
            return null;
        }
        int height = origin.getHeight();
        int width = origin.getWidth();
        float scale = ((float) newWidth) / width;
        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);
        Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
        return newBM;
    }


}

